package com.wy.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static final String timeformat1 = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String timeformat2 = "yyyy-MM-dd HH:mm:ss";
    public static final String timeformat3 = "yyyyMMddHHmmss";
    public static final String timeformat4 = "yyyy-MM-dd";
    public static final String timeformat5 = "yyyy-MM";
    public static final String timeformat6 = "yyyy";

    public static String convertLongToDate(String longTime, String format){

        Date datetime = new Date(Long.parseLong(longTime)); // 根据long类型的毫秒数生命一个date类型的时间
        return new SimpleDateFormat(format).format(datetime); // 把date类型的时间转换为string
    }


    /**
     * 获取现在时间
     *
     * @return返回长时间格式 yyyy-MM-dd HH:mm:ss
     */
    public static Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }

    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringDate(Date date,String formatStr) {
        SimpleDateFormat bf = new SimpleDateFormat(formatStr);
        String s = bf.format(date);
        return s;
    }

    public static Date getFormatDate(Date date) {
        SimpleDateFormat bf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = bf.format(date);
        Date parse = null;
        try {
            parse = bf.parse(s);// df.parse(s);String转成对象
        } catch (Exception e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 时间转换
     * @param s
     * @return
     */
    public static Date getFormatDate(String s,String format){
        DateFormat bf = new SimpleDateFormat(format);
        Date parse = null;
        try {
            parse = bf.parse(s);// df.parse(s);String转成对象
        } catch (Exception e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获得指定日期的前几个月，后几个月
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedMonth(String specifiedDay,Integer month){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat(timeformat4).parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int currentMonth=c.get(Calendar.MONTH);
        c.set(Calendar.MONTH,currentMonth+month);

        String dayBefore=new SimpleDateFormat(timeformat4).format(c.getTime());
        return dayBefore;
    }

    /**
     * 获得指定日期的前几个年，后几个年
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedYear(String specifiedDay,Integer year){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat(timeformat4).parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int currentMonth=c.get(Calendar.YEAR);
        c.set(Calendar.YEAR,currentMonth+year);

        String dayBefore=new SimpleDateFormat(timeformat4).format(c.getTime());
        return dayBefore;
    }


    public static String getTime(String specifiedDay,String format){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat(format).parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        String dayBefore=new SimpleDateFormat(format).format(c.getTime());
        return dayBefore;
    }

    public static void main(String[] args) {
//        String monthBefore = getSpecifiedMonth("2018-12",4);
        String yearBefore = getSpecifiedYear("2018-12-12 22:22:22",4).substring(0,7);
//        System.out.println(monthBefore);
        System.out.println(yearBefore);
    }

}
